package com.mpush.core.chat;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.mpush.api.chat.ChatMessage;
import com.mpush.netty.http.HttpCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mpush.api.chat.ChatMessageDto;
import com.mpush.api.chat.ChatMessageSendResult;
import com.mpush.core.MPushServer;
import com.mpush.netty.http.HttpClient;
import com.mpush.netty.http.NettyHttpClientUtils;
import com.mpush.tools.common.DateUtils;
import com.mpush.tools.config.CC;

/**
 * 处理
 */
public class ChatMessageHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(ChatMessageHandler.class);

    private final HttpClient httpClient;

    public ChatMessageHandler(MPushServer mPushServer) {
        this.httpClient = mPushServer.getHttpClient();
    }


    public void updateOrSaveMsg(ChatMessage chatMessage, ChatMessageSendResult result) {
        ChatMessageDto chatMessageDto = convertFrom(chatMessage, result);
        String api = null != chatMessageDto.getId() ? CC.mp.api.updateMsg : CC.mp.api.saveMsg;
        post(api, chatMessageDto.toParam(), new ChatMessageHttpCallback(chatMessageDto));
    }

    private static ChatMessageDto convertFrom(ChatMessage chatMessage, ChatMessageSendResult result) {
        ChatMessageDto dto = new ChatMessageDto();

        dto.setId(chatMessage.getId());
        dto.setSessionId(chatMessage.getSessionId());
        dto.setSenderId(chatMessage.getSenderId());
        dto.setSenderName(chatMessage.getSenderName());
        dto.setReceiverId(chatMessage.getReceiverId());
        dto.setReceiverName(chatMessage.getReceiverName());
        dto.setType(chatMessage.getType());
        dto.setMsgContent(chatMessage.getMsgContent());
        String dateTime = DateUtils.format(new Date(chatMessage.getSendTime()), DateUtils.FULL_TIME_FORMAT);
        dto.setSendTime(dateTime);
        dto.setDuration(chatMessage.getDuration());

        dto.setSendStatue(result.value);
        dto.setReceiveStatus(result.value);
        if (result == ChatMessageSendResult.SUCCESS) {
            dto.setReceiveTime(dateTime);
        }

        return dto;
    }

    /**
     * 更新会话创建通知的状态为已通知
     *
     * @param notificationId
     */
    public void updateSessionNotified(Long notificationId) {

        Map<String, String> param = new HashMap<>(0);
        param.put("notificationId", null != notificationId ? notificationId.toString() : "");

        post(CC.mp.api.sessionNotified, param);

    }

    /**
     * 更新成员变动通知的状态为已通知
     *
     * @param notificationId
     */
    public void updateMemberNotified(Long notificationId) {

        Map<String, String> param = new HashMap<>(0);
        param.put("notificationId", null != notificationId ? notificationId.toString() : "");

        post(CC.mp.api.memberNotified, param);
    }

    private void post(String api, Map<String, String> param) {
        post(api, param, new DefaultHttpCallback());
    }

    private void post(String api, Map<String, String> param, HttpCallback callback) {

        String uri = "http://" + CC.mp.api.server_address + api;

        try {
            NettyHttpClientUtils.post(httpClient, uri, param, callback);
        } catch (Exception e) {
            LOGGER.error("", e);
        }
    }


}
